package martian.t1me.core
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	
	public class Group extends EventDispatcher implements Sequencable
	{
		public var stack:Vector.<Sequencable>;
		private var i:int = 0;
		
		public function Group(...args)
		{
			stack = new Vector.<Sequencable>();
			
			for each (var arg:* in args)
			{
				if (arg is Sequencable) { stack.push(arg); }
				else { trace("Warning:", arg, "does not implement Sequencable (skipped)"); }
			}
		}
		
		public function start():void
		{
			dispatchEvent(new Event(Sequence.START));
			
			for each (var item:Sequencable in stack)
			{
				item.addEventListener(Sequence.STOP, stop);
				item.start();
			}
		}
		
		private function stop(e:Event):void
		{
			e.target.removeEventListener(Sequence.STOP, stop);
			i++;
			
			if (i == stack.length) { dispatchEvent(new Event(Sequence.STOP)); }
		}
	}
}